package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.SuccessorProjectTDao;
import com.sykj.why.mongo.document.entity.*;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.*;
import com.sykj.why.mongo.vo.entity.SuccessorProjectTVo;
import com.sykj.why.util.BaseTools;
import com.sykj.why.util.DateTool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/12
 * @desc 传承人与项目业务实现层
 */

@Service("SuccessorAlbumTService")
public class SuccessorProjectTServiceImpl extends BasicServiceImpl<SuccessorProjectT> implements SuccessorProjectTService {

    @Autowired
    private SuccessorProjectTDao successorProjectTDao;
    @Autowired
    private SuccessorTService successorTService;
    @Autowired
    private AlbumTService albumTService;
    @Autowired
    private MenuCategoryTService menuCategoryTService;
    @Autowired
    private AuxiliaryCategoryTService auxiliaryCategoryTService;

    /**
     * 传承人与项目绑定
     * @param successorProjectT
     */
    @Override
    public void saveSuccessorProject(SuccessorProjectT successorProjectT) {
        verify(successorProjectT);
        //排序
        long count = successorProjectTDao.count(SuccessorProjectT.class, false);
        successorProjectT.setSort((int) count);
        successorProjectT.setCreateTime(DateTool.getSystemTime());
        successorProjectT.setUpdateTime(DateTool.getSystemTime());
        successorProjectTDao.save(successorProjectT);
    }

    /**
     * 验证项目与传承人是否重复绑定
     * @param successorProjectT
     */
    private void verify(SuccessorProjectT successorProjectT){
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).
                and("projectId").is(successorProjectT.getProjectId()).
                and("successorId").is(successorProjectT.getSuccessorId()));
        SuccessorProjectT successorAlbum = successorProjectTDao.findOne(query, SuccessorProjectT.class);
        if (successorAlbum!=null){
            throw new WhyException(ResultEnum.DO_NOT_REPEAT_OPERATIONS);
        }
    }

    /**
     * 解除传承人与项目绑定
     * @param id
     */
    @Override
    public void removeSuccessorProject(String id) {
        SuccessorProjectT successorProjectT = findById(id);
        successorProjectTDao.remove(successorProjectT);
    }

    /**
     * 修改项目传承信息
     * @param successorProjectT
     */
    @Override
    public void updateSuccessorProject(SuccessorProjectT successorProjectT) {
        verify(successorProjectT);
        SuccessorProjectT successorProject = findById(successorProjectT.getId());
        successorProject.setProjectId(successorProjectT.getProjectId());
        successorProject.setSuccessorId(successorProjectT.getSuccessorId());
        successorProject.setUpdateTime(DateTool.getSystemTime());
        successorProjectTDao.saveOrupdate(successorProject);
    }

    /**
     * 通过id查询绑定信息
     * @param id
     * @return
     */
    @Override
    public SuccessorProjectT findById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false));
        SuccessorProjectT successorProjectT = successorProjectTDao.findOne(query, SuccessorProjectT.class);
        if(successorProjectT !=null){
            return successorProjectT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 通过传承人或项目查询
     * @param projectId
     * @return
     */
    @Override
    public SuccessorProjectTVo findByProjectId(String projectId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).
                and("enable").is(true).and("projectId").is(projectId));
        SuccessorProjectT successorProjectT = successorProjectTDao.findOne(query, SuccessorProjectT.class);
        SuccessorProjectTVo vo = new SuccessorProjectTVo();
        if (successorProjectT != null){
            vo = getVo(vo, successorProjectT);
            //传承人列表
            List<SuccessorProjectT> list = successorProjectTDao.findAll(query, SuccessorProjectT.class);
            List<SuccessorT> successorTList = new LinkedList<>();
            if (list.size()>0){
                for (SuccessorProjectT successorAlbum : list){
                    SuccessorT successorT = successorTService.findById(successorAlbum.getSuccessorId());
                    successorTList.add(successorT);
                }
                vo.setSuccessorList(successorTList);
            }
        }
        return vo;
    }

    /**
     * 通过绑定ID查询传承人信息
     * @param successorProjectId
     * @return
     */
    @Override
    public SuccessorProjectTVo successorDetail(String successorProjectId) {
        SuccessorProjectT successorProjectT =
                successorProjectTDao.get(SuccessorProjectT.class,successorProjectId,false);
        SuccessorProjectTVo vo = new SuccessorProjectTVo();
        if (successorProjectT !=null){
            vo = getVo(vo, successorProjectT);
        }
        return vo;
    }

    /**
     * 查询传承人下的传承项目列表
     * @param successorId
     * @return
     */
    @Override
    public List<SuccessorProjectTVo> findListBySuccessor(String successorId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).
                and("enable").is(true).and("successorId").is(successorId));
        List<SuccessorProjectT> list = successorProjectTDao.findAll(query, SuccessorProjectT.class);
        List<SuccessorProjectTVo> voList = new ArrayList<>();
        if (list!=null && list.size()>0){
            for (SuccessorProjectT successorProjectT : list){
                SuccessorProjectTVo vo = new SuccessorProjectTVo();
                voList.add(getVo(vo, successorProjectT));
            }
        }
        return voList;
    }

    private SuccessorProjectTVo getVo(SuccessorProjectTVo vo, SuccessorProjectT successorProjectT){
        BeanUtils.copyProperties(successorProjectT, vo);
        //前端相关赋值
        //获取传承人相关信息
        SuccessorT successorT = successorTService.findById(successorProjectT.getSuccessorId());
        if (successorT!=null){
            vo.setSuccessorName(successorT.getRealName());
            vo.setIntro(successorT.getIntro());
        }
        //获取项目相关信息
        /*ProjectT projectT = albumTService.findById(successorProjectT.getProjectId());
        if (albumT!=null){
            vo.setAlbumName(albumT.getAlbumName());
            //通过专辑查询分类信息
            vo.setMenuCategoryId(albumT.getMenuCategoryId());
            MenuCategoryT menuCategoryT = menuCategoryTService.findById(albumT.getMenuCategoryId());
            vo.setMenuCategoryName(menuCategoryT.getCategoryName());
            //分类全称
            List<MenuCategoryT> fathers= menuCategoryTService.findFathers(albumT.getMenuCategoryId());
            String categoryAllName = "";
            if (fathers!=null){
                for (MenuCategoryT menuCategory : fathers){
                    categoryAllName += menuCategory.getCategoryName() + "-";
                }
            }
            vo.setCategoryAllName(categoryAllName.substring(0,categoryAllName.length()-1));
            vo.setAuxiliaryCategoryId(albumT.getAuxiliaryCategoryId());
            AuxiliaryCategoryT auxiliaryCategoryT = auxiliaryCategoryTService.findById(albumT.getAuxiliaryCategoryId());
            if (auxiliaryCategoryT!=null){
                vo.setAuxiliaryCategoryFatherName(auxiliaryCategoryTService.findById(auxiliaryCategoryT.getPid()).getCategoryName());
                vo.setAuxiliaryCategoryName(auxiliaryCategoryT.getCategoryName());
            }
        }*/
        vo.setCreateTime(BaseTools.longToString(successorProjectT.getCreateTime()));
        vo.setUpdateTime(BaseTools.longToString(successorProjectT.getUpdateTime()));
        return vo;
    }

}
